package com.xq.quartz;

import com.xq.model.service.ITBaseStationStateService;
import com.xq.model.service.ITBaseSurfMinService;
import com.xq.utils.DateUtils;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * 多线程定时任务
 *
 * @author xu
 * 2021/8/20 12:03
 */
//@Component注解用于对那些比较中立的类进行注释；
@Component
@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
public class ThreadScheduleTask {
    @Resource
    private ITBaseSurfMinService surfMinService;
    @Resource
    private ITBaseStationStateService stationStateService;

    /**
     * 定时执行搜索地面分钟要素
     * 5分钟一次
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void searchSurfMinBy5Min() throws IllegalAccessException, IOException, URISyntaxException {
        System.out.println("----------执行分钟要素-------------");
        System.out.println("五分钟执行一次");
        String times = DateUtils.dateTimeMinusFiveMin();
        System.out.println("请求时间:"+times);
        String adminCodes = "520100";
        System.out.println("请求区域:"+adminCodes);
        surfMinService.searchSurfByRegion(times,adminCodes);
    }


    /**
     * 定时执行分钟要素缺报信息
     * 5分钟一次
     * 从3分钟开始 错开分钟要素查询
     */
    @Scheduled(cron = "0 3/5 * * * ? ")
    public void againGetSurfMin() throws IllegalAccessException, IOException, URISyntaxException {
        System.out.println("----------查询分钟要素缺报-------------");
        System.out.println("五分钟执行一次");
        System.out.println("当前时间："+ DateUtils.getNowDateTime());
        // 超过3个小时就不执行了
        surfMinService.againGetSurf(3);
    }

    /**
     * 定时执行状态信息
     * 每到30分，执行一次
     */
    @Scheduled(cron = "0 30 * * * ?")
    public void  getStationState() throws IllegalAccessException, IOException, URISyntaxException {
        System.out.println("-----------执行站点状态------------");
        System.out.println("每到30分,执行一次");
        // 向前推 8个小时 获取世界时
        String times = DateUtils.dateTimeMinusHour(8);
        System.out.println("请求时间:"+times);
        stationStateService.getStationState(times);
    }

    /**
     * 定时执行站点状态缺报信息
     * 一个小时执行一次
     * 到整点执行
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void againGetStationState() throws IllegalAccessException, IOException, URISyntaxException {
        System.out.println("-----------查询站点状态缺报------------");
        System.out.println("每个小时,执行一次");
        System.out.println("当前时间："+ DateUtils.getNowDateTime());
        // 超过1天就不执行了
        stationStateService.againGetStationState(1);
    }
}
